package coffee.tigase.client;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Packet;

import coffee.tigase.constant.XMPPConfig;

/**
 * 
 * @author coffee
 * 
 *         2012-10-30上午10:16:46
 */
public class ChatMain {
	public static void main(String[] args) {

		Connection conn = XMPPConnectionFactory.create();

		final String otherJid = "2166401@" + XMPPConfig.HOST;
		
		try {
			conn.connect();
			//conn.getAccountManager().createAccount("admin", "admin");
			conn.login("admin", "admin");
			System.out.println("user_admin login success");
			Chat chat = conn.getChatManager().createChat(
					 otherJid, new MessageListener() {
						public void processMessage(Chat chat,
								org.jivesoftware.smack.packet.Message message) {
							System.out.println("Received message: " + message);
							System.out.println(message.getBody());
						}
					});

			chat.sendMessage("Hi buddy!");

			conn.addPacketListener(new PacketListener() {

				public void processPacket(Packet pack) {
					System.out.println("pack: " + pack.toXML());
					System.out.println(pack.getProperty("body"));
				}

			}, new PacketFilter() {
				public boolean accept(Packet arg0) {
					return true;
				}
			});

			System.out.println("ok");

			Thread.sleep(1000 * 1000);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			conn.disconnect();
		}
	}
}
